<?php

namespace App\Http\Controllers;

use App\Models\News;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    //
    public function index()
    {
        $news =News::orderBy('created_at', 'desc')->take(10)->get();
        return view('home',compact('news')); // 显示主页
    }

    public function new_list($page=1)
    {
        $perPage = 4;

        // 计算要跳过的记录数
        $skip = ($page - 1) * $perPage;
        // 获取指定页的数据
        $news = News::skip($skip)->take($perPage)->get();
        $nums = News::count();
        return view('new_list',compact('news','nums'));
    }

    public function contact()
    {
        return view('contact');
    }

    public function detail($id)
    {
        $new = News::findOrFail($id); // 根据ID获取帖子，若未找到则抛出404
        return view('detail', compact('new')); // 返回详情视图
    }
}
